package com.dingwen.treasure.business.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.dingwen.treasure.base.util.DateChineseUtil;
import com.dingwen.treasure.business.service.YueService;
import com.dingwen.treasure.mybatisplus.util.SysConfigUtil;
import com.dingwen.treasure.open.api.baidu.map.bo.WeatherBo;
import com.dingwen.treasure.open.api.baidu.map.config.BaiduMapConfig;
import com.dingwen.treasure.open.api.baidu.map.service.BaiduMapService;
import com.dingwen.treasure.open.api.pub.bo.MsgBo;
import com.dingwen.treasure.open.api.pub.bo.YueBo;
import com.dingwen.treasure.open.api.pub.config.WechatPubConfig;
import com.dingwen.treasure.open.api.pub.service.WechatPubService;
import com.dingwen.treasure.open.api.tx.night.config.TxNightConfig;
import com.dingwen.treasure.open.api.tx.rainbow.config.TxRainbowConfig;
import com.dingwen.treasure.open.api.tx.service.TxService;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 月服务impl
 *
 * @author dingwen
 * @date 2022/12/29
 */
@Service
public class YueServiceImpl implements YueService {


    /**
     * 百度地图服务
     */
    @Resource
    private BaiduMapService baiduMapService;

    @Resource
    private BaiduMapConfig baiduMapConfig;


    @Resource
    private SysConfigUtil sysConfigUtil;
    /**
     * 微信公众号服务
     */
    @Resource
    private WechatPubService wechatPubService;

    @Resource
    private WechatPubConfig wechatPubConfig;

    /**
     * 彩虹屁服务
     */
    @Resource
    private TxService rainbowService;

    @Resource
    private TxRainbowConfig txRainbowConfig;


    @Resource
    private TxService nightService;

    @Resource
    private TxNightConfig txNightConfig;

    @Scheduled(cron = "01 0 23 * * ?")
    @Override
    public void goodNight() {
        // 获取微信公众号token
        wechatPubService.fetchToken();
        // 获取open ids
        List<String> openIds = wechatPubService.fetchOpenIds();
        Assert.isTrue(CollUtil.isNotEmpty(openIds), "用户开发id空，发送失败");
        // 构建模版信息
        String night = nightService.fetchOne(txNightConfig.getNightUrl(), txNightConfig.getKey(), "晚安心语获取异常");
        YueBo yueBo = buildYueNightBo(night);
        openIds.forEach(openId -> {
                    //if("o7mPI5kAj24_qaBMpnWn-VI9XXiU".equals(openId)){
                    //    return;
                    //}
                    yueBo.setTouser(openId);
                    wechatPubService.pushTemplateMsgForYue(yueBo);
                });
    }

    @Scheduled(cron = "0 42 7 * * ?")
    @Override
    public void goodMorning() {
        // 获取微信公众号token
        wechatPubService.fetchToken();
        // 获取open ids
        List<String> openIds = wechatPubService.fetchOpenIds();
        Assert.isTrue(CollUtil.isNotEmpty(openIds), "用户开发id空，发送失败");
        // 获取天气信息
        WeatherBo weatherBo = baiduMapService.queryWeather(baiduMapConfig.getDistrictId());
        Assert.notNull(weatherBo, "天气信息获取异常");
        // 获取彩虹屁
        String rainbow = rainbowService.fetchOne(txRainbowConfig.getRainbowUrl(), txRainbowConfig.getKey(), "彩虹屁获取异常");
        Assert.isTrue(StrUtil.isNotBlank(rainbow), "彩虹屁获取异常");
        // 构建模版信息
        YueBo yueBo = buildYueMorningBo(weatherBo, rainbow);
        openIds.forEach(openId -> {
                    //if("o7mPI5kAj24_qaBMpnWn-VI9XXiU".equals(openId)){
                    //    return;
                    //}
                    yueBo.setTouser(openId);
                    wechatPubService.pushTemplateMsgForYue(yueBo);
                });

    }

    /**
     * 构建月早安模版
     *
     * @param weatherBo 天气数据
     * @param rainbow   彩虹
     * @return {@link YueBo}
     */
    private YueBo buildYueMorningBo(WeatherBo weatherBo, String rainbow) {
        YueBo yueBo = YueBo.builder()
                .tel(MsgBo.builder()
                        .value(rainbow)
                        .color("#FF1493")
                        .build())
                .together(MsgBo.builder()
                        .value(DateUtil.betweenDay(DateUtil.date(), new Date("2023/1/18"), false) + "")
                        .color("#FF1493")
                        .build())
                .now(MsgBo.builder()
                        .value(DateChineseUtil.date(DateUtil.date()))
                        .color("#1E90FF")
                        .build())
                .acquaintance(MsgBo.builder()
                        .value(DateUtil.betweenDay(DateUtil.date(), new Date("2022/10/4"), false) + "")
                        .color("#FF1493")
                        .build())
                .city(MsgBo.builder()
                        .value(weatherBo.getLocation())
                        .color("#1E90FF")
                        .build())
                .low(MsgBo.builder()
                        .value(weatherBo.getLow())
                        .color("#1E90FF")
                        .build())
                .high(MsgBo.builder()
                        .value(weatherBo.getHigh())
                        .color("#1E90FF")
                        .build())
                .weather(MsgBo.builder()
                        .value(weatherBo.getWeather())
                        .color("#1E90FF")
                        .build())
                .birthday(MsgBo.builder()
                        .color("#EEE8AA")
                        .value(DateUtil.betweenDay(DateUtil.date(),
                                new Date(sysConfigUtil.getValue(YUE_BIRTHDAY_NL__GL_CONFIG_KEY)), false) + "")
                        .build())
                .template_id(wechatPubConfig.getYueMorningTemplateId())
                .build();
        Map<String, MsgBo> data = new HashMap<>(7);
        data.put(NOW_KEY, yueBo.getNow());
        data.put(TEL_KEY, yueBo.getTel());
        data.put(CITY_KEY, yueBo.getCity());
        data.put(LOW_KEY, yueBo.getLow());
        data.put(ACQUAINTANCE_KEY, yueBo.getAcquaintance());
        data.put(WEATHER_KEY, yueBo.getWeather());
        data.put(HIGH_KEY, yueBo.getHigh());
        data.put(BIRTHDAY_KEY, yueBo.getBirthday());
        data.put(TOGETHER_KEY, yueBo.getTogether());
        return yueBo.setData(data);
    }


    /**
     * 构建月晚安
     *
     * @return {@link YueBo}
     */
    private YueBo buildYueNightBo(String goodNight) {
        YueBo yueBo = YueBo.builder()
                .goodNight(
                        MsgBo.builder()
                                .color("#FF1493")
                                .value(goodNight)
                                .build()
                )
                .template_id(wechatPubConfig.getYueGoodNightTemplateId())
                .build();
        Map<String, MsgBo> data = new HashMap<>(2);
        data.put(GOOD_NIGHT_KEY, yueBo.getGoodNight());
        return yueBo.setData(data);
    }
}
